package com.cos.hospital.ui.usercenter.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cos.hospital.R;
import com.cos.hospital.model.bean.user.SchBean;
import com.cos.hospital.tool.UTxt;

/**
 * @Description: 我的排班
 * @Author: Kosmos
 * @Date: 2018年09月16日 00:05
 * @Email: KosmoSakura@foxmail.com
 */
public class SchGridWeekAdapter extends BaseAdapter {
    public static final int T_WEEK = 1; //1：周
    public static final int T_CLASS = 2; //2：分类
    public static final int T_SELECT = 3; //3:排班
    private SchBean[] data;//数据
    private int selectPosition;
    private Context context;//上下文

    public SchGridWeekAdapter(SchBean[] data, Context context) {
        this.data = data;
        this.context = context;
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    @Override
    public int getCount() {
        return data.length;
    }

    @Override
    public SchBean getItem(int position) {
        return data[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_sch_grid, parent, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        SchBean bean = data[position];
        //1：周，2：分类,3:排班
        if (bean == null) {
            holder.layWeek.setVisibility(View.GONE);
            holder.tClass.setVisibility(View.VISIBLE);
            holder.tClass.setText("");
            holder.layRoot.setBackgroundResource(R.drawable.rect_sch);
        } else {
            if (bean.getType() == T_WEEK) {
                holder.layWeek.setVisibility(View.VISIBLE);
                holder.tClass.setVisibility(View.GONE);
                holder.tWeek.setText(bean.getName());
                if (UTxt.isEmpty(bean.getDate())) {
                    holder.tDate.setVisibility(View.GONE);
                } else {
                    holder.tDate.setVisibility(View.VISIBLE);
                    holder.tDate.setText(bean.getDate());
                }
                holder.layRoot.setBackgroundResource(R.drawable.rect_sch);
            } else {
                holder.layWeek.setVisibility(View.GONE);
                holder.tClass.setVisibility(View.VISIBLE);
                if (bean.getType() == T_CLASS) {
                    holder.tClass.setText(bean.getName());
                    holder.layRoot.setBackgroundResource(R.drawable.rect_sch);
                } else {
                    holder.tClass.setText(bean.isSelect() ? "班" : "");
                    if (bean.isFuture()) {
                        if (position == selectPosition) {
                            holder.layRoot.setBackgroundResource(R.drawable.rect_rim_orange);
                        } else {
                            holder.layRoot.setBackgroundResource(R.drawable.rect_sch);
                        }
                    } else {
                        holder.layRoot.setBackgroundResource(R.drawable.rect_sch_gray);
                    }
                }
            }
        }

        return view;
    }

    private class ViewHolder {
        final TextView tClass, tWeek, tDate;
        final View layRoot, layWeek;

        ViewHolder(View root) {
            layRoot = root.findViewById(R.id.item_sch_root);
            tClass = root.findViewById(R.id.item_sch_class);
            tWeek = root.findViewById(R.id.item_sch_week);
            tDate = root.findViewById(R.id.item_sch_date);
            layWeek = root.findViewById(R.id.item_sch_week_lay);
        }
    }
}
